/*
 * Created by JFormDesigner on Thu Jun 23 12:33:01 CST 2022
 */

package jw3.c10.view;

import java.awt.event.*;

import jw3.c10.controllner.GoodsController;
import jw3.c10.controllner.OrderController;
import jw3.c10.utils.DBConnection;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class Jindex extends JFrame {
    public Jindex() {
        initComponents();
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(800,600);
    }
        //定义全局变量
    Vector<Vector<Object>> data;

    //
    private void showData(String name) {
        String sql="select * from goods where GoodsName like ?";
        Connection conn= null;
        try {
            conn = DBConnection.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,"%"+name+"%");
            ResultSet rs=ps.executeQuery();
            data=new Vector<Vector<Object>>();
            while (rs.next()){
                Vector<Object> data1=new Vector<Object>();

                data1.add(rs.getInt("BarCode"));
                data1.add(rs.getString("GoodsName"));
                data1.add(rs.getString("GoodsPrice"));
                data1.add(rs.getString("GoodsPlace"));
                data1.add(rs.getInt("GoodsNum"));


                data.add(data1);

            }
            DBConnection.close(rs,ps,conn);

            Vector<String> heads=new Vector<String>();

            heads.add("礼品编号");
            heads.add("礼品名字");
            heads.add("所需积分");
            heads.add("原产地");
            heads.add("剩余数量");


            DefaultTableModel dtm=new DefaultTableModel(data,heads);
            table1.setModel(dtm);

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here

        showData(textField1.getText());
        if (data.size()==0){
            JOptionPane.showMessageDialog(null,"暂无数据！");
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
        dialog1.setVisible(true);
    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
        int index =table1.getSelectedRow();
        if (index!=-1){
            if (JOptionPane.showConfirmDialog(null,"您确认要兑换吗？")==0){
                String sql="update goods set GoodsNum=GoodsNum-1 where BarCode=?";
                int id=(int)data.get(index).get(0);

                Connection conn= null;
                try {
                    conn = DBConnection.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                try {

                    PreparedStatement ps=conn.prepareStatement(sql);
                    ps.setInt(1,id);
                    if (ps.executeUpdate()>0){
                        int t=Integer.parseInt(label4.getText());
                        int d= GoodsController.getGoodsNum(id);
                    if (t>d){
                        showData("");
                        JOptionPane.showMessageDialog(null,"兑换成功");

                        int h=Integer.parseInt(data.get(index).get(2).toString());
                        int w=t-h;
                        label4.setText(""+w);

                    }else {
                        JOptionPane.showMessageDialog(null,"积分不足！");
                    }
                    }else {
                        JOptionPane.showMessageDialog(null,"兑换失败");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }

        }else {
            JOptionPane.showMessageDialog(null,"您未选择任意一个");
        }
    }

    private void button5MouseClicked(MouseEvent e) {
        // TODO add your code here

        String sql="SELECT c.UserName,b.GoodsName,a.Amount FROM `order` a LEFT JOIN goods b ON a.UserID=b.UserID\n" +
                "LEFT JOIN userinfo c ON a.UserID=c.UserID";

        try {
            Connection conn=DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            data =new Vector<Vector<Object>>();
            while (rs.next()){
                Vector<Object> data1=new Vector<Object>();
                data1.add(rs.getString("UserName"));
                data1.add(rs.getString("GoodsName"));
                data1.add(rs.getInt("Amount"));

                data.add(data1);

            }
            DBConnection.close(rs,ps,conn);
            Vector<String> heads=new Vector<String>();
            heads.add("用户");
            heads.add("礼品");
            heads.add("数量");
            DefaultTableModel dtm=new DefaultTableModel(data,heads);
            table2.setModel(dtm);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        dialog2.setModal(true);
        dialog2.setVisible(true);
    }

    private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here
        dialog1.dispose();
    }

    private void button6MouseClicked(MouseEvent e) {
        // TODO add your code here
        new Consume();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button2 = new JButton();
        label3 = new JLabel();
        label4 = new JLabel();
        button6 = new JButton();
        dialog1 = new JDialog();
        label2 = new JLabel();
        textField2 = new JTextField();
        button3 = new JButton();
        button4 = new JButton();
        dialog2 = new JDialog();
        label5 = new JLabel();
        panel2 = new JPanel();
        scrollPane2 = new JScrollPane();
        table2 = new JTable();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u540d\u79f0\uff1a");
        contentPane.add(label1);
        label1.setBounds(50, 55, 75, 25);
        contentPane.add(textField1);
        textField1.setBounds(130, 52, 150, textField1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u641c\u7d22");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(325, 52), button1.getPreferredSize()));

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(5, 0, 410, 380);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(75, 130), panel1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u5151\u6362");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(445, 52), button2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u79ef\u5206\u4f59\u989d:");
        contentPane.add(label3);
        label3.setBounds(25, 95, 75, 25);

        //---- label4 ----
        label4.setText("0");
        contentPane.add(label4);
        label4.setBounds(115, 95, 65, 25);

        //---- button6 ----
        button6.setText("\u8fd4\u56de");
        button6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button6MouseClicked(e);
            }
        });
        contentPane.add(button6);
        button6.setBounds(new Rectangle(new Point(315, 95), button6.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);

            //---- label2 ----
            label2.setText("\u8bf7\u9009\u62e9\u5151\u6362\u6570\u91cf:");
            dialog1ContentPane.add(label2);
            label2.setBounds(10, 25, 125, 45);

            //---- textField2 ----
            textField2.setText("1");
            dialog1ContentPane.add(textField2);
            textField2.setBounds(new Rectangle(new Point(145, 30), textField2.getPreferredSize()));

            //---- button3 ----
            button3.setText("\u786e\u5b9a");
            button3.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button3MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button3);
            button3.setBounds(new Rectangle(new Point(50, 105), button3.getPreferredSize()));

            //---- button4 ----
            button4.setText("\u53d6\u6d88");
            button4.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button4MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button4);
            button4.setBounds(new Rectangle(new Point(170, 105), button4.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }

        //======== dialog2 ========
        {
            Container dialog2ContentPane = dialog2.getContentPane();
            dialog2ContentPane.setLayout(null);

            //---- label5 ----
            label5.setText("\u6211\u7684\u793c\u54c1");
            dialog2ContentPane.add(label5);
            label5.setBounds(65, 5, 240, 45);

            //======== panel2 ========
            {
                panel2.setLayout(null);

                //======== scrollPane2 ========
                {
                    scrollPane2.setViewportView(table2);
                }
                panel2.add(scrollPane2);
                scrollPane2.setBounds(new Rectangle(new Point(0, 0), scrollPane2.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel2.getComponentCount(); i++) {
                        Rectangle bounds = panel2.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel2.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel2.setMinimumSize(preferredSize);
                    panel2.setPreferredSize(preferredSize);
                }
            }
            dialog2ContentPane.add(panel2);
            panel2.setBounds(new Rectangle(new Point(60, 85), panel2.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog2ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog2ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog2ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog2ContentPane.setMinimumSize(preferredSize);
                dialog2ContentPane.setPreferredSize(preferredSize);
            }
            dialog2.pack();
            dialog2.setLocationRelativeTo(dialog2.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        //积分
        label4.setText(""+Consume.JNum);

    }



    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button2;
    private JLabel label3;
    private JLabel label4;
    private JButton button6;
    private JDialog dialog1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button3;
    private JButton button4;
    private JDialog dialog2;
    private JLabel label5;
    private JPanel panel2;
    private JScrollPane scrollPane2;
    private JTable table2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
